﻿package ua.mihaylenko.cardsplugin.colorpicker{
	/**
	Color Picker Icon class 
	
	@author Sharwan Kami <sharwan@gmail.com>
	@version 1.0
	@history 2009-05-10
	
	* 
	* This file is part of E-Card plugin, from zetaprints.com
	* 
	
	@usage
	Activate with:
		<code>
			var cpi=new CPI(this,_cImage);
			addChild(cpi);
		</code>
	
	*/
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.geom.ColorTransform;
	import fl.transitions.easing.Regular;
	import fl.transitions.Tween;
	import flash.net.navigateToURL;
	import flash.net.URLRequest;
	import ua.mihaylenko.cardsplugin.common.DocumentClass;
	import ua.mihaylenko.cardsplugin.components.thumbs.ThumbNovigate;
	import ua.mihaylenko.cardsplugin.components.cardeditor.ECEImage;
	
	public class CPI extends Sprite{
		public var cp:ZColorPicker;//colourpicker instance
		public var activeColor=0xffffff;//currently selected colour
		public var colorMC:Sprite;
		public var _selectIcon:MovieClip;
		public var active:Boolean=false;//if the icon is selected or not
		private var cpOpen:Boolean=false;//if colourpicker is open or not
		private var border:Sprite;//border to icon
		private var thumbN:ThumbNovigate;//instance of ThumbNovigate class
		private var cimage:ECEImage;//instance of ecard editor image class
		private var thumbNToolTip;//instance of tooltip object of ThumbNovigate class instance
		
		function CPI(t,c){
			_selectIcon.visible=false;
			thumbN=t;
			cimage=c;
			
			
			//if some colour has been selected selected image field, set that colour as active colour
			if(cimage.cpColor){
				var myPattern:RegExp = /#/gi;  
				activeColor=uint(String(cimage.cpColor).replace(myPattern,"0x"));
			}			
			
			//update the icon colour
			var newColorTransform:ColorTransform = colorMC.transform.colorTransform;
			newColorTransform.color = uint(activeColor);
			colorMC.transform.colorTransform = newColorTransform;
			
			buttonMode=true;
			
			//create the white border around icon, it will become red with width 2 on select
			border=new Sprite();
			border.graphics.lineStyle(1,0xffffff);
			border.graphics.drawRect(0,0,80,54);
			addChild(border);
			
			//if no stock image is selected for selected image field and 
			//colour is assigned by colour picker previously
			//make the icon selected
			if(int(cimage.selectIndex)<0){
				if(cimage.cpColor){
					select=true;
				}
			}
			
			addEventListener(MouseEvent.CLICK,onMouseClick);
			//addEventListener(MouseEvent.ROLL_OVER,onRollover);
			//addEventListener(MouseEvent.ROLL_OUT,onRollout);
		}
		
		
		private function onRollover(e){
			//on rollover of the icon, update the tooptip text
			thumbNToolTip=thumbN._cThumbTooltip.text;
			thumbN._cThumbTooltip.text="Choose Colour";
			
			if(active)return;			
			new Tween(this,"y",Regular.easeIn,0,-10,0.2,true);
		}
		
		private function onRollout(e){
			//on rollout, set back the previous tooltip text
			thumbN._cThumbTooltip.text=thumbNToolTip;
			
			if(active)return;
			new Tween(this,"y",Regular.easeIn,this.y,0,0.2,true);
		}
		
		private function onMouseClick(e){
			if(cpOpen)return;
			
			//make the icon selected
			select=true;
			
			//deselect other thumbs (stock images)
			try{
				thumbN.selectedItem.y=0;
				thumbN.selectedItem.select=false;
			}catch(e){}
			
			//if coloour picker is already there, remove it
			try{
				removeChild(cp);
			}catch(e){}
			
			//create new instance of colour picker
			cp=new ZColorPicker();			
			
			cp.addEventListener(cp.COLOR_SELECT,onColorSelected);
			cp.addEventListener(cp.COLOR_PICKER_CLOSE,onCPClosed);			
			
			var xp=(DocumentClass.AppStage.stageWidth*.5)-160;
			var yp=(DocumentClass.AppStage.stageHeight*.5)-120;
			
			if(thumbN._cpX){
				xp=thumbN._cpX;
			}
			
			if(thumbN._cpY){
				yp=thumbN._cpY;
			}
			
			cp.showColorPicker(activeColor,xp,yp);
			cpOpen=true;
			
			//add the colour picker instance on top of the stage, to bring it into top layer
			DocumentClass.AppStage.addChild(cp);
			
			//add the listener to stage to check if its clicked outside colorpicker
			stage.addEventListener(MouseEvent.CLICK,onStageClick);
		}
		
		private function onStageClick(e){
			try{
			if((e.target==cp) || (cp.contains(e.target)) || (e.target==this) || (this.contains(e.target))){
				//nothing to do if its clicked on cp or icon
			}else{
				//close the color picker and remove listener for outside click
				onCPClosed(null);
				stage.removeEventListener(MouseEvent.CLICK,onStageClick);
			}
			}catch(e){}
		}
		
		private function onColorSelected(e){
			//update the icon colour
			var newColorTransform:ColorTransform = colorMC.transform.colorTransform;
			newColorTransform.color = uint(cp.color);
			colorMC.transform.colorTransform = newColorTransform;
			
			//set the selected colour as active colour
			activeColor=cp.color;
			
			//set the selected colour as colour of the image field
			cimage.cpColor="#"+ZColor.getRGB(activeColor);
			
			//close the colour picker
			onCPClosed(null);
		}
		
		private function onCPClosed(e){
			//remember the co-ordinates where the colour picker is closed
			thumbN._cpX=cp.x;
			thumbN._cpY=cp.y;
			
			cpOpen=false;
			
			cimage.cpColor="#"+ZColor.getRGB(activeColor);
			DocumentClass.AppStage.removeChild(cp);
			//stage.removeEventListener(MouseEvent.CLICK,onStageClick);
		}
		
		public function set select(val:Boolean){
			if(val==true){
				border.graphics.clear();
				border.graphics.lineStyle(3,0xff0000);
				border.graphics.drawRect(0,0,80,54);
				active=true;
				
				//set the selected index as negative, so it can be decided, no stock image thumbs are selected
				cimage.selectIndex=-1;
				_selectIcon.visible=true;
			}else{
				border.graphics.clear();
				border.graphics.lineStyle(1,0xffffff);
				border.graphics.drawRect(0,0,80,54);
				_selectIcon.visible=false;
				this.y=0;
				try{
					cpOpen=false;
					DocumentClass.AppStage.removeChild(cp);
				}catch(e){}
				active=false;
			}
		}
		
		public function getCP(){
			return cp;
		}
	}
}